# -*- coding: UTF-8 -*-
# @create  : 2024/8/1 14:11
# @Author  : 天空之城
# @File    : invalid_address.py
import re

from address_normalizer.app.common.response import AddressParameterError, InvalidAddressError


class InvalidAddress:
    def __init__(self):
        self.valid_keywords = ["省", "市", "区", "县", "路", "道", "街", "镇", "乡", "村"]
        self.AddressParameterError = AddressParameterError.Message
        self.InvalidAddressError = InvalidAddressError.Message

    def contains_valid_keywords(self, address):
        return any(keyword in address for keyword in self.valid_keywords)

    def is_valid_address(self, address):
        # 地址不能为空
        if not address:
            return self.AddressParameterError
        # # 地址中必须包含有效关键字
        # if not self.contains_valid_keywords(address):
        #     return InvalidAddressError.Message
        # 地址长度校验
        if len(address) < 3 or len(address) > 200:
            return self.InvalidAddressError

        # 地址不能是纯数字或重复字符
        if re.match(r'^\d+$', address):  # 全是数字
            return self.InvalidAddressError

        if len(set(address)) == 1:  # 全是同一个字符
            return self.InvalidAddressError

        # 检查地址中的数字比例
        num_digits = sum(c.isdigit() for c in address)
        if num_digits / len(address) > 0.5:  # 如果数字超过50%
            return self.InvalidAddressError

        # 检查地址中是否有连续重复的字符
        if re.search(r'(.)\1{4,}', address):  # 三个或更多连续重复字符
            return self.InvalidAddressError

        return address
